package com.qfedu.controller;

import com.qfedu.model.*;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
@author LiJH
@date 2023-02-10
*/
@RestController
@RequestMapping("project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private StageService stageService;
    @Autowired
    private SubitemService subitemService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private TaskplanService taskplanService;
    @Autowired
    private BranchService branchService;
    @PostMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = projectService.findPage(queryPageBean);
        return page;
    }
   @PostMapping("addProject")
    public Result addProject(@RequestBody Project project){
         int i = projectService.addProject(project);
        Result result = new Result();
        if (i>0){
            result.setFlag(true);
            result.setMessage("添加成功！");
        }else {
            result.setFlag(false);
            result.setMessage("添加失败！");
        }
       return result;
    }
    @GetMapping("findById")
    public Result findById( int pid){
        Project byId = projectService.findById(pid);
        //查询成功
        if (ObjectUtils.notEqual(byId, null)) {
            return Result.successResult("查询成功",byId);
        } else {
            return Result.failResult("查询失败");
        }
    }
    @GetMapping("findAll")
    @CrossOrigin
    public List<Project> findAll(){
        List<Project> all = projectService.findAll();
        return all;
    }

    @PostMapping("editProject")
    public Result editProject(@RequestBody Project project){
        Result result = new Result();
        int i = projectService.editProject(project);
        if (i>0){
            result.setFlag(true);
            result.setMessage("更新成功！");
        }else {
            result.setFlag(false);
            result.setMessage("更新失败！");
        }
        return result;
    }

    @GetMapping("deleteByPid")
    public Result deleteByPid(int pid ){
        int i = projectService.deleteByPid(pid);
        Result result = new Result();
        if (i>0){
            result.setFlag(true);
            result.setMessage("删除成功！");
        }else {
            result.setFlag(false);
            result.setMessage("删除失败！");
        }
        return result;
    }

    @GetMapping("index")
    @CrossOrigin
    public Map<String,Object> index(int pid){
        List<Stage> stages = stageService.findByPid(pid);
        Project index = projectService.index(pid);
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("stages",stages);
        stringObjectMap.put("proAndDevCon",index);
        return stringObjectMap;
    }
    @GetMapping("cname")
    @CrossOrigin
    public List<Subitem> cname(int pid,String cid){
        List<Subitem> byPid = subitemService.findByPid(pid);

        return byPid;
    }
    @GetMapping("item")
    @CrossOrigin
    public List<Item> item(String suid){
        return itemService.findBySuid(suid);
    }
    @GetMapping("bname")
    @CrossOrigin
    public List<Branch> bname(int pid,String did){

        return branchService.findByPid(pid);
    }
    @GetMapping("taskplan")
    @CrossOrigin
    public List<Taskplan> taskplans(String bid){

        return taskplanService.findByBid(bid);
    }
}